#!/usr/bin/env python
# -*- coding: utf-8 -*-

from PySide import QtCore, QtGui
import clever_rc
from UserLetters import UserLetters
from Map import Map
from classes import Alphabet
        
class User():
    def __init__(self, type=0, name=None):
        self.type = type
        self.user_name = name
        self.user_letters = UserLetters()
        self.score = 0
        
    def __getitem__(self, item):
        if item == "name":
            return self.user_name
        else:
            return None
        
    def setLetter(self, letter):
        self.user_letters.append(letter)
        
    def getLetters(self):
        return [l['l'] for l in self.user_letters]
        
        
class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        #graphics widgets
        self.frame = QtGui.QFrame()
        self.frame_right = QtGui.QFrame()
        self.frameHLayout = QtGui.QHBoxLayout(self.frame)
        self.v_layout = QtGui.QVBoxLayout()
        self.push_layout = QtGui.QHBoxLayout()
        self.list_users = QtGui.QListWidget()
        self.list_users.setDisabled(True)
        self.list_letters = QtGui.QListWidget()
        self.list_letters.setMaximumHeight(25)
        self.list_letters.setDragEnabled(True)
        self.list_letters.setFlow(QtGui.QListView.Flow.LeftToRight)
        self.push_ready = QtGui.QPushButton(u"Готово", self)
        self.push_change = QtGui.QPushButton(u"Обменять", self)
        
        self.map = Map()
        self.users = []
        self.current_user = None
        self.current_user_index = None
        self.timer = QtCore.QTimer()
        self.timer_interval = 60000
        self.game_is_started = False
        self.alphabet = Alphabet()
        
        self.setupMenus()
        self.setupWidgets()
        self.initSignals()

        self.setSizePolicy( \
            QtGui.QSizePolicy( \
                QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed))
        self.setWindowTitle(u"Умник")
    
    @QtCore.Slot(dict)
    def slot(self, dict):
        print "Map say slot fnc"#, self.UserLetters.sayUserLetters(), dict
        
    @QtCore.Slot()
    def timeout(self):
        self.current_user_index += 1
        self.current_user_index %= len(self.users)
        self.list_users.setCurrentRow(self.current_user_index)
        self.list_letters.clear()
        self.list_letters.addItems(\
            self.users[self.current_user_index].getLetters())
        print "timeout"
    
    @QtCore.Slot()
    def click_push_ready(self):
        self.timer.stop()
        self.timeout()
        self.timer.start(self.timer_interval)
        
        
    def initSignals(self):
        self.map.clicked.connect(self.slot)
        self.timer.timeout.connect(self.timeout)
        self.push_ready.clicked.connect(self.click_push_ready)
        
    def setupMenus(self):
        gameMenu = self.menuBar().addMenu("&Game")

        newAction = gameMenu.addAction("&New...")
        newAction.setShortcut("Ctrl+N")

        restartAction = gameMenu.addAction("&Restart")

        exitAction = gameMenu.addAction("E&xit")
        exitAction.setShortcut("Ctrl+Q")

        aboutMenu = self.menuBar().addMenu("&About")

        newAction.triggered.connect(self.newGame)
        restartAction.triggered.connect(self.restartGame)
        exitAction.triggered.connect(QtGui.qApp.quit)
        
    def setupWidgets(self):
        self.frameHLayout.addWidget(self.map)
        self.frameHLayout.addLayout(self.v_layout)
        self.v_layout.addWidget(self.list_users)
        self.v_layout.addWidget(self.list_letters)
        self.v_layout.addLayout(self.push_layout)
        
        self.push_layout.addWidget(self.push_ready)
        self.push_layout.addWidget(self.push_change)
        self.setCentralWidget(self.frame)
        
    def newGame(self):
        if self.game_is_started:
            return
        self.timer.start(self.timer_interval)
        self.game_is_started = True
        self.current_user_index = 0
        self.users = [User(0, u"Человечище"), User(0, u"Компьютерный моск"), \
                      User(0, u"Гена"), User(0, u"Чебурашка")]
        self.list_users.addItems([u['name'] for u in self.users])
        for u in self.users:
            u.user_letters = [self.alphabet.get(), self.alphabet.get(), \
                              self.alphabet.get(), self.alphabet.get(), \
                              self.alphabet.get(), self.alphabet.get(), \
                              self.alphabet.get()]
        self.list_users.setCurrentRow(self.current_user_index)
        self.list_letters.addItems(\
            self.users[self.current_user_index].getLetters())
    
    def restartGame(self):
        pass


if __name__ == "__main__":
    import sys
    userLetters = UserLetters()
                
    app = QtGui.QApplication(sys.argv)
    window = MainWindow()
    #window.openImage(':/images/example.jpg')
    window.show()
    sys.exit(app.exec_())
 
